﻿using MediatR;
using ProcurementManagement.Applcation.Command.PurchaseReturnsCommand;
using ProcurementManagement.Infrstructre.Interface;

namespace ProcurementManagement.Applcation.CommandHanlers.PurchaseReturnsCommandHandler
{
    /// <summary>
    /// 删除实退采购款列表中介者
    /// </summary>
    public class DelDirectRefundCommandHandler:IRequestHandler<DelDirectRefundCommand,int>
    {
        private readonly lnterfaceDirectRefundRepository _directRefundRepository;

        public DelDirectRefundCommandHandler(lnterfaceDirectRefundRepository directRefundRepository)
        {
            _directRefundRepository = directRefundRepository;
        }

        public async Task<int> Handle(DelDirectRefundCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            foreach (var item in request.ids)
            {
                var res = (await _directRefundRepository.GetAll()).First(x => x.DirectRefundId == item);
                if (res != null)
                {
                    res.IsDel = true;
                    res.DelName = request.DelName;
                    res.DelTime = DateTime.Now;
                    await _directRefundRepository.UpdataAsync(res);
                    count = 1;
                }
                else
                {
                    count = -1;//不存在
                }
            }
            return count;
        }
    }
}
